package base.view;

import base.entity.info.DirectoryInfo;
import base.entity.info.FileInfo;
import base.entity.info.Info;
import base.entity.regitem.DirRegItem;
import base.entity.regitem.RegItem;
import base.entity.serach.SearchResult;
import base.service.DirectoriesServer;
import base.service.impl.DirectoriesServerImpl;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * create by lyl
 * 2019.11.30
 * 目录的视图
 */
public class DirectoriesView extends View{


    private List<RegItem> childs; // 该目录下的文件与目录的视图

    // 通过一个目录登记项创建视图对象
    public DirectoriesView(RegItem item, String path,List<RegItem> childs){
        super.item = item;
        super.isFile = false;
        super.path = path;
        this.childs = childs;
    }

    public List<RegItem> getChilds() {
        return childs;
    }

    // 获取名字
    public String getName(){
        return item.getName();
    }
    // 获取子目录信息
    public List<Info> getChildDirInfo(){
        List<Info> infos = new ArrayList<Info>();
        for (RegItem regItem : childs){
            if ((regItem.getProperty() & NOT_FILE) > 0){
                Info info = new DirectoryInfo(super.path,regItem.getName());
                infos.add(info);
            }
        }
        return infos;
    }
    // 获取子文件信息
    public List<FileInfo> getChildFileInfo(){
        List<FileInfo> infos = new ArrayList<FileInfo>();
        for (RegItem regItem : childs){
            if ((regItem.getProperty() & NOT_FILE) == 0){
                FileInfo info = new FileInfo(super.path,regItem.getName(),regItem.getType(),regItem.getLength());
                infos.add(info);
            }
        }
        return infos;
    }

    /**
     *
     * @param index 搜索关键词
     * @return 搜索结果list
     */
    public List<SearchResult> doSearch(String index) throws IOException {
        DirectoriesServer server = new DirectoriesServerImpl();
//        System.err.println("this.path = " + this.path);
        int blockNum = server.getBlockNum(this.path);

        List<SearchResult> results = new DirectoriesServerImpl().serachRegItem(index,blockNum);
        for (SearchResult result :results){ // 拼装成绝对路径
            String path = this.path + result.getPath();
            path = path.replaceAll("//","/");
            result.setPath(path);
        }
        return results;
    }
    public List<FileInfo> serach2FileInfo(List<SearchResult> searchResults){
        List<FileInfo> infos = new ArrayList<FileInfo>();
        for (SearchResult result : searchResults){
            if (result.isFile()){
                String baseUrl = result.getPath().indexOf("/") >= 0  ? result.getPath().substring(0,result.getPath().lastIndexOf("/")) : "";
//                System.out.println("文件info url:" + baseUrl);
                FileInfo fileInfo = new FileInfo(baseUrl,result.getName() + "    ("+result.getPath()+")",result.getRegItem().getType().toString(),result.getRegItem().getLength());
                infos.add(fileInfo);
            }
        }
        return infos;
    }
    public List<Info> serach2DirectoriesInfo(List<SearchResult> searchResults){
        List<Info> infos = new ArrayList<Info>();
        for (SearchResult result : searchResults){
            if (!result.isFile()){
                String baseUrl = result.getPath().indexOf("/") >= 0 ? result.getPath().substring(0,result.getPath().lastIndexOf("/")) : "";
//                System.out.println("目录info url:" + baseUrl);
                DirectoryInfo info = new DirectoryInfo(baseUrl,result.getName() + "    ("+result.getPath()+")");
                infos.add(info);
            }
        }
        return infos;
    }

    public static void main(String[] args) throws IOException {
        DirectoriesServer server = new DirectoriesServerImpl();
        DirectoriesView directoriesView = server.dir("111");
        List<FileInfo> infos = directoriesView.getChildFileInfo();
        for (FileInfo info:infos)System.out.println(info);
    }
}
